package com.education.platform.management.service.impl;

import com.education.platform.common.exception.AssertException;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.SysRoleDao;
import com.education.platform.dao.SysRoleMenuDao;
import com.education.platform.dao.entity.SysRoleDO;
import com.education.platform.dao.entity.SysRoleMenuDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.SysRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* describe: 角色
* @author : 
* @date: 2020-06-30 09:05:21.385
**/
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleDao, SysRoleDO> implements SysRoleService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
	private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdateRole(SysRoleDO roleDO) {
        if(roleDO == null){
            AssertException.throwBusinessEp("数据为空");
        }
        if(roleDO.getId()==null){
            roleDO.setCreateBy(ThreadUser.getUserName());
            roleDO.setCreateTime(new Date());
            roleDO.setDeleted(false);
            if(sysRoleDao.insert(roleDO)!=1){
                throw new BusinessException("新增失败");
            }
        }else{
            roleDO.setUpdateBy(ThreadUser.getUserName());
            roleDO.setUpdateTime(new Date());
            if(sysRoleDao.updateById(roleDO)!=1){
                throw new BusinessException("修改失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Integer id) {
        SysRoleDO roleDO = new SysRoleDO();
        roleDO.setDeleted(true);
        if(sysRoleDao.updateById(roleDO)!=1){
            AssertException.throwBusinessEp("删除失败");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRoleMenu(Integer roleId, List<Integer> menuIds) {
        sysRoleMenuDao.deleteByRoleId(roleId);
        List<SysRoleMenuDO> list = new ArrayList<>(menuIds.size());
         Date time = new Date();
        if(CollectionUtils.isNotEmpty(menuIds)){
            SysRoleMenuDO roleMenuDO;
          for(Integer menuId:menuIds){
              roleMenuDO = new SysRoleMenuDO();
              roleMenuDO.setMenuId(menuId);
              roleMenuDO.setRoleId(roleId);
              roleMenuDO.setCreateTime(time);
              list.add(roleMenuDO);
          }
            sysRoleMenuDao.insertInBatch(list);
        }
        }
}
